//
//  CZHomeTopItem.m
//  QQ空间
//
//  Created by zhangping on 16/2/2.
//  Copyright © 2016年 zhangping. All rights reserved.
//

#import "CZTopItem.h"

@interface CZTopItem ()

/// 分割线
@property (strong, nonatomic) UIView *separatorView;

/// 图片按钮
@property (strong, nonatomic) UIButton *iconButton;

/// 大标题
@property (strong, nonatomic) UILabel *titleLabel;

/// 小标题
@property (strong, nonatomic) UILabel *subTitleLabel;
@end

@implementation CZTopItem

+ (instancetype)topItemWithIcon:(NSString *)icon title:(NSString *)title subTitle:(NSString *)subTitle {
    CZTopItem *topItem = [[CZTopItem alloc] init];
    [topItem setIcon:icon];
    [topItem setTitle:title];
    [topItem setSubTitle:subTitle];
    topItem.frame = CGRectMake(0, 0, 100, 44);
    
    return topItem;
}

- (instancetype)init {
    if (self = [super init]) {
        [self prepareUI];
    }
    
    return self;
}

- (void)prepareUI {
    // 添加子控件
    [self addSubview:self.separatorView];
    [self addSubview:self.iconButton];
    [self addSubview:self.titleLabel];
    [self addSubview:self.subTitleLabel];
    
    // 添加约束
    [self.separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self);
        make.top.equalTo(self).offset(12);
        make.bottom.equalTo(self).offset(-12);
        make.width.equalTo(@1);
    }];
    
    [self.iconButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self).offset(36);
        make.top.equalTo(self).equalTo(@5);
    }];
    
    [self.subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.titleLabel);
        make.bottom.equalTo(self).equalTo(@-5);
    }];
}

- (void)setIcon:(NSString *)icon {
    NSString *highlightedImageName = [NSString stringWithFormat:@"%@_highlighted", icon];
    [self.iconButton setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [self.iconButton setImage:[UIImage imageNamed:highlightedImageName] forState:UIControlStateHighlighted];
}

- (void)setTitle:(NSString *)title {
    self.titleLabel.text = title;
}

- (void)setSubTitle:(NSString *)subTitle {
    self.subTitleLabel.text = subTitle;
}

#pragma mark - 懒加载
- (UIView *)separatorView {
    if (_separatorView == nil) {
        _separatorView = [[UIImageView alloc] init];
        
        _separatorView.backgroundColor = [UIColor blackColor];
        _separatorView.alpha = 0.2;
    }
    
    return _separatorView;
}

- (UIButton *)iconButton {
    if (_iconButton == nil) {
        _iconButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _iconButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        
        _iconButton.imageEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    }
    
    return _iconButton;
}

- (UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        
        _titleLabel.font = [UIFont systemFontOfSize:12];
    }
    
    return _titleLabel;
}

- (UILabel *)subTitleLabel {
    if (_subTitleLabel == nil) {
        _subTitleLabel = [[UILabel alloc] init];
        
        _subTitleLabel.font = [UIFont systemFontOfSize:12];
    }
    
    return _subTitleLabel;
}

// 添加点击事件
- (void)addTarget:(id)target action:(SEL)action {
    [self.iconButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}
@end
